#
# Copyright 2017, Data61
# Commonwealth Scientific and Industrial Research Organisation (CSIRO)
# ABN 41 687 119 230.
#
# This software may be distributed and modified according to the terms of
# the BSD 2-Clause license. Note that NO WARRANTY is provided.
# See "LICENSE_BSD2.txt" for details.
#
# @TAG(DATA61_BSD)
#

cmake_minimum_required(VERSION 3.5.1)

project(libmuslc NONE)

set(LIB_MUSLC_PARALLEL_BUILD true CACHE BOOL "Use `make -j` when building libmuslc")

# Map KernelSel4Arch to muslc architecture.
set(
    arch_mapping # element format: "KernelSel4Arch[:muslc_arch]"
    "ia32:i386"
    "x86_64" # use KernelSel4Arch
    "aarch32:arm"
    "arm_hyp:arm" # arm_hyp is legacy hack that might get removed one day
    "aarch64" # use KernelSel4Arch
    "riscv32:riscv" # no difference between 32/64 bit
    "riscv64:riscv" # no difference between 32/64 bit
)
# Find the tuple with the mapping information. If KernelSel4Arch is not set at
# all, matching will fail.
if(NOT ";${arch_mapping};" MATCHES ";${KernelSel4Arch}(:([^;]*))?;")
    message(FATAL_ERROR "unsupported KernelSel4Arch: '${KernelSel4Arch}'")
elseif(CMAKE_MATCH_2)
    set(muslc_arch "${CMAKE_MATCH_2}")
else()
    set(muslc_arch "${KernelSel4Arch}")
endif()

message(
    STATUS "${PROJECT_NAME} architecture: '${muslc_arch}' (from KernelSel4Arch '${KernelSel4Arch}')"
)

set(
    LIB_MUSLC_GLOB_SOURCES OFF
    CACHE BOOL "This flag causes CMake to add lots of source files to its dependency lists
     which slows down its configuration times.
     When not making changes in the musllibc directory, it is likely not
     necessary to have this enabled."
)
mark_as_advanced(LIB_MUSLC_PARALLEL_BUILD LIB_MUSLC_GLOB_SOURCES)
if(LIB_MUSLC_GLOB_SOURCES)
    file(
        GLOB_RECURSE
            deps
            **/*.c
            **/*.h
            **/*.S
            **/*.s
    )
endif()

set(parallel "")
if(LIB_MUSLC_PARALLEL_BUILD)
    include(ProcessorCount)
    ProcessorCount(N)
    if(N EQUAL 0)
        message(WARNING "Could not detect ProcessorCount for building musllibc.")
    else()
        # We set tasks to number of CPUs + 2 to account for blocking IO, this is
        # a similar heuristic to what Ninja uses by default. Note that this gets
        # used in a rule generated by add_custom_command and is additive with
        # any existing Ninja jobs.
        math(EXPR Nplus2 "${N} + 2")
        set(parallel "-j${Nplus2}")
    endif()
endif()

# We make an attempt to extract compiler options from CMake
get_property(compile_options DIRECTORY PROPERTY COMPILE_OPTIONS)

# Also inherit the CMAKE build type flags from one of the following variables
# depending on the configured build type:
# - CMAKE_C_FLAGS_DEBUG
# - CMAKE_C_FLAGS_RELEASE
# - CMAKE_C_FLAGS_RELWITHDEBINFO
# - CMAKE_C_FLAGS_MINSIZEREL
string(TOUPPER CMAKE_BUILD_TYPE build_type)
set(cmake_built_type_config_name CMAKE_C_FLAGS_${build_type})
separate_arguments(
    cmake_c_flags_sep NATIVE_COMMAND "${CMAKE_C_FLAGS};${${cmake_built_type_config_name}}"
)

list(APPEND compile_options "${cmake_c_flags_sep}")

# Add the target triple to the compile flags if we are using clang
# It is not part of the CMAKE_C_FLAGS variable
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
    list(APPEND compile_options "${CMAKE_C_COMPILE_OPTIONS_TARGET}${CMAKE_C_COMPILER_TARGET}")
endif()

include(memoize)
# memoize this installation rule which will save the resulting artifact in a
# cache and reuse it across builds. This will rebuild from source if the git
# directory has changes or has a changed commit hash.
memoize_add_custom_command(
    musllibc
    "${CMAKE_CURRENT_BINARY_DIR}/build-temp/stage"
    "${CMAKE_CURRENT_SOURCE_DIR}"
    "${compile_options} ${muslc_arch}"
    ""
    OUTPUT
    build-temp/stage/lib/libc.a
    # If we have to rebuild, first clear the temporary build directory as we
    # have no correctly captured the output files or dependencies
    COMMAND
    rm
    -r
    build-temp
    COMMAND
    mkdir
    -p
    build-temp
    COMMAND
    ${CMAKE_COMMAND}
    -E
    env
    TOOLPREFIX=${CROSS_COMPILER_PREFIX}
    C_COMPILER=${CMAKE_C_COMPILER}
    # Suppress make output by default. Verbose output can now be achieved by
    # "MAKEFLAGS=V3 ninja". "${MAKEFLAGS:--s}" expands to "-s" if MAKEFLAGS is
    # unset.
    "MAKEFLAGS=\"\$\${MAKEFLAGS:--s}\""
    # TODO: should also pass in the cflags for the CMAKE_BUILD_TYPE ?
    NK_CFLAGS="${compile_options}"
    TARGET=${muslc_arch}
    make
    -C
    build-temp
    ${parallel}
    -f
    "${CMAKE_CURRENT_SOURCE_DIR}/Makefile"
    "STAGE_DIR=${CMAKE_CURRENT_BINARY_DIR}/build-temp/stage"
    "SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}"
    DEPENDS
    ${deps}
    COMMAND_EXPAND_LISTS
    COMMENT
    "Invoking muslc build system"
)

add_custom_target(muslc_gen DEPENDS build-temp/stage/lib/libc.a)

add_library(muslc_imported STATIC IMPORTED GLOBAL)
add_dependencies(muslc_imported muslc_gen)
set_property(
    TARGET muslc_imported
    PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/build-temp/stage/lib/libc.a"
)

add_library(muslc INTERFACE)
add_dependencies(muslc muslc_imported)
set_property(TARGET muslc PROPERTY INTERFACE_LINK_LIBRARIES muslc_imported)
target_include_directories(muslc INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/build-temp/stage/include")
